package com.gitee.wsl.doc.rss

import com.gitee.wsl.doc.rss.utils.toPrefixNamedElement
import com.gitee.wsl.ext.any.isNotNull

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class ParseTest {

    private  var feed: RSSFeedObject = ParseRSS.parse( Feed.rssV2EnUS)


    @Test
    fun rssNamePrefixedCheck() {
        assertTrue("item".toPrefixNamedElement().prefix.isEmpty())
        assertEquals("item".toPrefixNamedElement().name,"item")
        assertEquals("xmlns:rdf".toPrefixNamedElement().prefix,"xmlns")
        assertEquals("xmlns:rdf".toPrefixNamedElement().name,"rdf")
    }

//    @Test
//    fun validRSSFeedReader() {
//        val feed1 = ParseRSS.parse(xml, false) {
//            RSSFeedObject()
//        }
//        val feed2: RSSFeedObject = ParseRSS.parse(StringReader(xml))
//        assertThat(feed1.title).matches("AAAA - RSS Channel - International Edition")
//        assertThat(feed2.title).matches("AAAA - RSS Channel - International Edition")
//    }

    @Test
    fun validateRSSFeed() {
        assertEquals(feed.title,"AAAA - RSS Channel - International Edition")
        assertEquals(feed.link,"https://dp3ap2.jogjaprov.go.id/")
        assertTrue(feed.publishDate==null)
        assertTrue(feed.image.isNotNull())
        assertEquals(feed.image!!.url,"https://static01.nyt.com/images/misc/NYT_logo_rss_250x40.png")
        assertEquals(feed.language,"en-us")
        assertEquals(feed.items.size,2)
    }

    @Test
    fun validateRSSItem() {
        val item1 = feed.items[0]
        assertEquals(item1.title,"Puncak Peringatan Hari Anak Nasional D.I. Yogyakarta 2018")
        assertEquals(item1.link,"http://dp3ap2.jogjaprov.go.id/berita/detail/246-puncak-peringatan-hari-anak-nasional-han-d-i-yogyakarta-2018")
        assertEquals(item1.publishDate,"05 Agustus 2019")
        assertTrue(item1.description!!.contains("Yogyakarta, BPPM- Anak adalah"))
        assertEquals(item1.media.size,1)
        assertEquals(item1.media[0].credit,"Leo Correa/Associated Press")
        assertEquals(item1.author!!.name,"John Doe")
        assertEquals(item1.category.size,2)
        assertEquals(item1.category[1].domain,"http://www.nytimes.com/namespaces/keywords/nyt_geo")
        val item2 = feed.items[1]
        assertTrue(item2.guId.isNotNull())
        assertFalse(item2.guId!!.isPermaLink)
        assertEquals(item2.media.size,11)
        assertEquals(item2.media[10].height,144)
    }
}